# -*- coding: utf-8 -*-

"""
PyoCDVD (http://code.google.com/p/pyocdvd/)
Media Organizer (CD, DVD, etc)

Developers:
    Bruno Pitteli Gonçalves <scorninpc(*)gmail(*)com>
    Paul Eipper <lkraider(*)gmail(*)com>

Licence: GNU GENERAL PUBLIC LICENSE v3

Credits:
    Código de acesso ao HAL do Linux baseado no código disponível em:
    http://dev.burnstation.org/cgi-bin/trac.cgi/file/burnstation/burnstation-client/import/DriveSelector.py?rev=190&format=txt

"""

import os
import sys
import dircache
import string

NT = (os.name == 'nt')
POSIX = (os.name == 'posix')

if NT:
    try:
        import win32file
    except ImportError, e:
        sys.stdout.writelines('Erro ao importar o modulo win32file')
else:
    import dbus


class PyoCDVD_Cdrom:
    def __init__(self):
        if POSIX:
            self.bus = dbus.SystemBus()
            hal_obj = self.bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
            hal = dbus.Interface(hal_obj, 'org.freedesktop.Hal.Manager')
            self.udis = hal.FindDeviceByCapability('volume')

    def list_devices(self):
        drivers = []
        if NT:
            for i in string.uppercase:
                if win32file.GetDriveType(i + ":") == win32file.DRIVE_CDROM:
                    drivers.append(i + ":")
        elif POSIX:
            for udi in self.udis:
                parent = self._get_udi_parent(udi)
                if parent.GetProperty('storage.hotpluggable') or 'storage.cdrom' in parent.GetProperty('info.capabilities'):
                    device = self.dev.GetProperty('block.device')
                    if 'storage.cdrom' in parent.GetProperty('info.capabilities'):
                        drivers.append(device)
        return drivers

    def list_files(self, root):
        root_list = dircache.listdir(root)
        file_list = []
        for filename in root_list:
            if not type(filename) == unicode:
                filename = unicode(filename, sys.getfilesystemencoding())
            subdir = root + '/' + filename
            if os.path.isdir(subdir):
                subdir_list = self.list_files(subdir)
                file_list.append(subdir + '/')
                file_list += subdir_list
            else:
                file_list.append(subdir)
        return file_list
            
    def mount_point(self, dispositivo):
        if not POSIX:
            return None
        for udi in self.udis:
            parent = self._get_udi_parent(udi)
            name = self.dev.GetProperty('volume.label') or parent.GetProperty('info.product')
            device = self.dev.GetProperty('block.device')
            if dispositivo == device:
                return self.dev.GetProperty('volume.mount_point')

    def _get_udi_parent(self, udi):
        dev_obj = self.bus.get_object('org.freedesktop.Hal', udi)
        self.dev = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device')
        parent_obj = self.bus.get_object('org.freedesktop.Hal', self.dev.GetProperty('info.parent'))
        parent = dbus.Interface(parent_obj, 'org.freedesktop.Hal.Device')
        return parent